package com.pine.service.core;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.collection.CollUtil;
import com.pine.manager.pineframework.SysMenuManager;
import com.pine.manager.pineframework.SysRoleManager;
import com.pine.manager.pineframework.SysRoleMenuManager;
import com.pine.manager.pineframework.SysRoleUserManager;
import com.pine.manager.pineframework.bo.SysMenuBo;
import com.pine.manager.pineframework.bo.SysRoleBo;
import com.pine.manager.pineframework.bo.SysRoleMenuBo;
import com.pine.manager.pineframework.bo.SysRoleUserBo;
import com.pine.manager.pineframework.query.SysRoleMenuQuery;
import com.pine.manager.pineframework.query.SysRoleUserQuery;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 获取角色列表&权限列表 服务实现类
 * </p>
 *
 * @author pine manage
 * @since 2024-08-15
 */
@Slf4j
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private SysRoleManager sysRoleManager;

    @Resource
    private SysRoleUserManager sysRoleUserManager;

    @Resource
    private SysRoleMenuManager sysRoleMenuManager;

    @Resource
    private SysMenuManager sysMenuManager;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 通过用户ID查角色ID
        List<Long> roleIds = getRoleIds(loginId);
        if (CollUtil.isEmpty(roleIds)) {
            return new ArrayList<>();
        }

        // 查角色对应的菜单信息
        SysRoleMenuQuery sysRoleMenuQuery = new SysRoleMenuQuery();
        sysRoleMenuQuery.setRoleIds(roleIds);
        List<SysRoleMenuBo> sysRoleMenuBos = sysRoleMenuManager.list(sysRoleMenuQuery);
        if (CollUtil.isEmpty(sysRoleMenuBos)) {
            return new ArrayList<>();
        }

        List<Long> menuIds = sysRoleMenuBos.stream().map(SysRoleMenuBo::getMenuId).distinct().toList();
        if (CollUtil.isEmpty(menuIds)) {
            return new ArrayList<>();
        }

        // 根据菜单列表查菜单信息
        List<SysMenuBo> sysMenuBos = sysMenuManager.listByPrimaryKeys(menuIds);
        if (CollUtil.isEmpty(sysMenuBos)) {
            return new ArrayList<>();
        }

        // 获取菜单对应的权限
        return sysMenuBos.stream().map(SysMenuBo::getPermission).collect(Collectors.toList());
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 通过用户ID查角色ID
        List<Long> roleIds = getRoleIds(loginId);
        if (CollUtil.isEmpty(roleIds)) {
            return new ArrayList<>();
        }

        List<SysRoleBo> sysRoleBos = sysRoleManager.listByPrimaryKeys(roleIds);
        if (CollUtil.isEmpty(sysRoleBos)) {
            return new ArrayList<>();
        }

        // 返回角色编码
        return sysRoleBos.stream().map(SysRoleBo::getCode).collect(Collectors.toList());
    }

    /**
     * 获取角色ID列表
     *
     * @param loginId 登录ID
     * @return 角色ID列表
     */
    private List<Long> getRoleIds(Object loginId) {
        // 查用户角色关联关系
        SysRoleUserQuery sysRoleUserQuery = new SysRoleUserQuery();
        sysRoleUserQuery.setUserId(Long.valueOf(loginId.toString()));
        List<SysRoleUserBo> sysRoleUserBos = sysRoleUserManager.list(sysRoleUserQuery);
        if (CollUtil.isEmpty(sysRoleUserBos)) {
            return new ArrayList<>();
        }

        return sysRoleUserBos.stream().map(SysRoleUserBo::getRoleId).toList();
    }
}
